package com.chinemit.cui.components.container.group
{
	import flash.display.DisplayObject;

	public class TileGroup extends Group
	{
		private static const ORIENTATION_ROWS:String = "rows";
		private static const ORIENTATION_COLUMNS:String = "columns";
		
		private var orientation:String = ORIENTATION_ROWS; // rows \ columns
		
		private var _columnWidth:Number; //列宽
		private var _rowHeight:Number; 	 //行高
		
		private var _horizontalAlign:String; //水平方向布局 justify \ left \ right \center
		private var _verticalAlign:String;   //垂直方向布局   justify \ top \ bottom \ middle
		
		private var _horizontalGap:Number = 5; //水平方向间距 
		private var _verticalGap:Number = 5;   //垂直方向间距
		
		private var gWidth:Number;
		private var gHeight:Number;
		
		
		public function TileGroup(_w:Number=400,_h:Number=400){
			super();
			gWidth = _w;
			gHeight = _h;
		}
	 	
		public function get rowHeight():Number
		{
			return _rowHeight;
		}

		public function set rowHeight(value:Number):void
		{
			_rowHeight = value;
		}

		public function get columnWidth():Number
		{
			return _columnWidth;
		}

		public function set columnWidth(value:Number):void
		{
			_columnWidth = value;
		}

		public function get verticalAlign():String
		{
			return _verticalAlign;
		}

		public function set verticalAlign(value:String):void
		{
			_verticalAlign = value;
		}

		public function get horizontalAlign():String
		{
			return _horizontalAlign;
		}

		public function set horizontalAlign(value:String):void
		{
			_horizontalAlign = value;
		}

		public function get verticalGap():Number
		{
			return _verticalGap;
		}

		public function set verticalGap(value:Number):void
		{
			_verticalGap = value;
			refurbish();
		}

		public function get horizontalGap():Number
		{
			return _horizontalGap;
		}

		public function set horizontalGap(value:Number):void
		{
			_horizontalGap = value;
			refurbish();
		}

		/**
		 * 
		 * 
		 */		
		override protected function refurbish():void{
			
			super.refurbish();
			
//			this.graphics.clear();
			
			var tempChild:DisplayObject;
			
			var maxWith:Number = 0;
			var maxHeight:Number = 0;
			
			for(var i:int=0;i<this.numChildren;i++){
				tempChild = this.getChildAt(i);
				maxWith<tempChild.width?maxWith = tempChild.width:maxWith= maxWith;
				maxHeight<tempChild.height?maxHeight = tempChild.height:maxHeight= maxHeight;
			}
			
			maxWith+=horizontalGap;
			maxHeight+=verticalGap;
			
			var tmp:Number = gWidth/maxWith;
			
			var sumColumn:int = gWidth/maxWith
				
			tmp = (tmp - sumColumn)/(tmpColumn-1);
			maxWith+=tmp;	
				
			var tmpRow:int = 0;
			var tmpColumn:int = 0;
				
			for(i=0;i<this.numChildren;i++){
				
				tmpColumn = i%sumColumn;
				if(tmpColumn==0 && i!=0){
					tmpRow++;
				}
				tempChild = this.getChildAt(i);
				tempChild.x = tmpColumn*maxWith + (maxWith-horizontalGap- tempChild.width)*0.5;
				tempChild.y = tmpRow*maxHeight+(maxHeight-verticalGap-tempChild.height)*0.5;
//				this.graphics.beginFill(0xf8f8f8,i%2==0?0.8:1);
//				this.graphics.drawRect(tmpColumn*maxWith,tmpRow*maxHeight,maxWith-horizontalGap,maxHeight-verticalGap);
//				this.graphics.endFill();	
			}	
			 
		}
	}
}